﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Needle.DevToolkit.Win32
{

    internal enum KeyCode : int
    {
        LButton = 0x01,
        RButton = 0x02,
        MButton = 0x04,
        Shift = 0x10,
        Control = 0x11,
        Alt = 0x12,
        CapsLock = 0x14,
        NumLock = 0x90,
        Scroll = 0x91,
    }

    [Flags]
    internal enum KeyState : ushort
    {
        On = 0x0001,
        Down = 0x0080,
        HighMask = 0xff00,
        LowMask = 0x00ff,
    }

    internal class UnsafeNativeMethods
    {
        /// <remarks>
        /// Interop marshalling is pretty neat, you can use enums with the right
        /// basetype as parameter and return value type, and it works just fine!
        /// The actual signature is: <code>SHORT GetKeyState(int nVirtKey)</code>
        /// See also http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx
        /// </remarks>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention=CallingConvention.Winapi)]
        private static extern ushort GetKeyState(int keyCode);


        public static KeyState GetKeyState(KeyCode keyCode)
        {
            ushort state = GetKeyState((int)keyCode);
            return (KeyState)(state & (ushort)KeyState.LowMask);
        }

    }

    public static class Keyboard
    {

        public static bool CapsLock
        {
            get
            {
                KeyState state = UnsafeNativeMethods.GetKeyState(KeyCode.CapsLock);
                return (state & KeyState.On) == KeyState.On;
            }
        }

        public static bool NumLock
        {
            get
            {
                KeyState state = UnsafeNativeMethods.GetKeyState(KeyCode.NumLock);
                return (state & KeyState.On) == KeyState.On;
            }
        }

        public static bool ScrollLock
        {
            get
            {
                KeyState state = UnsafeNativeMethods.GetKeyState(KeyCode.Scroll);
                return (state & KeyState.On) == KeyState.On;
            }
        }

        public static bool ShiftKeyDown
        {
            get
            {
                KeyState state = UnsafeNativeMethods.GetKeyState(KeyCode.Shift);
                return (state & KeyState.Down) == KeyState.Down;
            }
        }

        public static bool CtrlKeyDown
        {
            get
            {
                KeyState state = UnsafeNativeMethods.GetKeyState(KeyCode.Control);
                return (state & KeyState.Down) == KeyState.Down;
            }
        }

        public static bool AltKeyDown
        {
            get
            {
                KeyState state = UnsafeNativeMethods.GetKeyState(KeyCode.Alt);
                return (state & KeyState.Down) == KeyState.Down;
            }
        }

    }

}
